package ${namespacePackage};

import ${entityPackage}.${entityName};
import com.google.common.collect.Maps;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.spirit.common.model.Pager;
import com.spirit.mybatis.BaseDao;
import com.spirit.user.User;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

<#list columns as column>
    <#if keyColumn?? && column.dbColumn == keyColumn><#assign keyJavaType=column.colJavaType keyField=column.field></#if>
</#list>
@Repository
public class ${daoName} extends SqlSessionDaoSupport {

    <#if keyColumn??>
    public Integer update(${entityName} ${entityParam}) {
        return getSqlSession().update("${entityName}.update", ${entityParam});
    }


    public Integer insert(${entityName} ${entityParam}) {
        return getSqlSession().insert("${entityName}.insert", ${entityParam});
    }


    public List<${entityName}> findAll() {
        return getSqlSession().selectList("${entityName}.findAll");
    }


    public ${entityName} findById(${keyJavaType} ${keyField}) {
        return getSqlSession().selectOne("${entityName}.findById", ${keyField});
    }


    public Pager<${entityName}> findByPage(Map<String, Object> params, int offset, int limit) {
        Long total = getSqlSession().selectOne("${entityName}.count", params);
        if (total == 0) {
        return Pager.empty(${entityName}.class);
        }
        Map<String, Object> paramMap = Maps.newHashMap();
        if (!params.isEmpty()) {
        paramMap.putAll(params);
        }
        paramMap.put("offset", offset);
        paramMap.put("limit", limit);
        List<${entityName}> data = getSqlSession().selectList("${entityName}.pager", paramMap);
        return new Pager<${entityName}>(total, data);
    }


    public Integer delete(${entityName} ${entityParam}) {
        return getSqlSession().delete("${entityName}.delete", ${entityParam});
    }
    </#if>
}